<?php


namespace app\jiangnan\controller;


use app\service\jiangnanService;
use app\utils\JwtUtils;
use think\Controller;
use think\Db;

class Alipay extends Controller
{

    //获取支付宝用户的user_id
    public function getUserId()
    {
        $api = new jiangnanService();
        //获取当前车辆支付宝ID
        $code = request()->get('auth_code');
        $oauthtoken = $api->xcxoauthToken($code);
        if(property_exists($oauthtoken,'sub_msg')){
            return json(['msg' => $oauthtoken->sub_msg,'code'=>"500"]);
        }

        $user_id = $oauthtoken->user_id;//user_id
        //return json(['info' => $user_id]);

        //
        $userInfo=Db::table("userinfo")->where("userid",$user_id)->find();
        if(empty($userInfo)){
            $arr["bindRoomStatus"]=false;
            $arr["userid"]=$user_id;
            $token=JwtUtils::getToken($arr);
            return json(["code"=>0,'accessToken' => $token,"bindRoomStatus"=>false]);
        }else{
            $arr["bindRoomStatus"]=false;
            if(!empty($userInfo["room_id"])){
                $arr["bindRoomStatus"]=true;
            }

            $arr=array_merge($arr,$userInfo);
            $token=JwtUtils::getToken($arr);
            return json(["code"=>0,'accessToken' => $token,"bindRoomStatus"=>$arr["bindRoomStatus"]]);
        }
//        $arr['info']=$user_id;
//        $res=json_encode($arr);
//        return $res;
    }
}